package dipl.algorithm.test.compare;

import dipl.algorithm.yap.YapUtilities;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apfloat.Apfloat;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author Marko Walther
 */
public class CalculateYapDeltas {

    public CalculateYapDeltas() {
    }

		@BeforeClass
		public static void setUpClass() throws Exception {
			maxN = 5;
			maxM = 5;
			maxL = 8;
			delta1 = new Apfloat[maxN*maxM*maxL];
			delta3 = new Apfloat[maxN*maxM*maxL];
			delta4 = new Apfloat[maxN*maxM*maxL];
		}

		@AfterClass
		public static void tearDownClass() throws Exception {
			// create and open html file
			String outDir = "statistics/comparison/results/yap_deltas/";
			if( new File( outDir ).exists() == false )
				new File( outDir ).mkdirs();
			SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd_HH-mm-ss" );
			File file = new File( outDir+format.format( new Date() )+".html" );
			BufferedWriter writer = new BufferedWriter( new FileWriter( file ) );

			writer.write( "<html><head><style type=\"text/css\">p,td,th { font-size:small;}</style></head><body>" );
			writer.write( "<h3>Berechnung der Quadrate der Yap-Schranken.</h3>" );
			writer.write( "<p>L=1.."+maxL+",n=1.."+maxN+", m=1.."+maxM+"</p>" );

			writer.write( "<p><b>Ergebnisse:</b></p>" );
			writer.write( "<table border=\"1\">" );
			writer.write( "<thead><tr>" );
			writer.write( "<th><b>L:</b></th>" );
			writer.write( "<th><b>m:</b></th>" );
			writer.write( "<th><b>n:</b></th>" );
			writer.write( "<th><b>Delta 1 (quadriert):</b></th>" );
			writer.write( "<th><b>Delta 3 (quadriert):</b></th>" );
			writer.write( "<th><b>Delta 4 (quadriert):</b></th>" );
			writer.write( "</tr></thead>" );
			writer.write( "<tbody>" );
			int i = 0;
			for( int L = 1; L <= maxL; L++ ) {
				for( int N = 1; N <= maxN; N++ ) {
					for( int M = 1; M <= maxM; M++ ) {
						writer.write( "<tr>" );
						writer.write( "<td>"+L+"</td>" );
						writer.write( "<td>"+N+"</td>" );
						writer.write( "<td>"+M+"</td>" );
						writer.write( "<td>"+delta1[i].toRadix(10).toString()+"</td>" );
						writer.write( "<td>"+delta3[i].toRadix(10).toString()+"</td>" );
						writer.write( "<td>"+delta4[i].toRadix(10).toString()+"</td>" );
						writer.write( "</tr>" );
            i++;
          }
				}
			}
			writer.write( "</tbody>" );
			writer.write( "</table>" );
			writer.write( "</body></html>" );
			writer.close();
		}

		/**
		 *
		 */
    @Test
    public void CalculateDeltas() {
			int i = 0;
			for( int L = 1; L <= maxL; L++ ) {
				for( int N = 1; N <= maxN; N++ ) {
					for( int M = 1; M <= maxM; M++ ) {
						Apfloat deltas[] = YapUtilities.CalculateSquareDeltas( L, N, M );
						delta1[i] = deltas[0];
						delta3[i] = deltas[1];
						delta4[i] = deltas[2];
						i++;
					}
				}
			}
		}

		//
		// MEMBERS
		//

		/** maximum degree of first curve */
		private static int maxN;
		/** maximum degree of second curve */
		private static int maxM;
		/** maximum bit complexity of control coefficients */
		private static int maxL;

		/** deltas 1 calculated */
		private static Apfloat[] delta1;
		/** deltas 3 calculated */
		private static Apfloat[] delta3;
		/** deltas 4 calculated */
		private static Apfloat[] delta4;
}